package org.example.mySwing;

import org.example.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Set;

public class MyFrame extends JFrame {
    private JPanel panel;
    private ButtonGroup buttonGroup;
    private JCheckBox[] checkBoxes;
    private JButton startButton;
    private JButton exitButton;
    private JButton showTextBoxButton;
    public static JTextArea textArea;
    Main main = new Main();
    HashMap<String, Object> suffixMap = new HashMap<>();
    {
        suffixMap.put("、gif、jpg、png、psd、bmp、", "图片类型");
        suffixMap.put("、wmv、asf、rm、 rmvb、mov、mp4、avi、", "视频类型");
        suffixMap.put("、txt、doc、wps、docx、xls、xlsx、pdf、", "文档类型");
        suffixMap.put("、wav、mp3、", "音频内型");
        suffixMap.put("、rar、zip、z、", "压缩包类型");
    }

    public static StringBuffer suffixNum = new StringBuffer() ;
    public MyFrame() {
        super("搜盘程序");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口在屏幕中央显示
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((screenSize.getWidth() - getWidth()) / 2);
        int y = (int) ((screenSize.getHeight() - getHeight()) / 2);
        setLocation(x, y);
        // 创建面板
        panel = new JPanel(new GridLayout(0, 4));

        // 创建多选框
        Set<String> strings = suffixMap.keySet();
        checkBoxes = new JCheckBox[strings.size()];
        int i = 0;
        for (String suffix : strings) {
            checkBoxes[i] = new JCheckBox(String.valueOf(suffixMap.get(suffix)));
            checkBoxes[i].setActionCommand(suffix);
            panel.add(checkBoxes[i]);
            i++;
        }
        // 创建按钮
        startButton = new JButton("Start");
        exitButton = new JButton("Exit");

        // 创建文本框
        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        // 添加事件处理
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 启动程序
                for (JCheckBox checkBox : checkBoxes) {
                    if (checkBox.isSelected()) {
                       suffixNum.append(checkBox.getActionCommand()) ;
                    }
                }
                main.startAll();
                textArea.append("Start button clicked\n");
            }
        });

        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });


        // 设置 "X" 按键为退出程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 使用 BorderLayout 管理器
        setLayout(new BorderLayout());

        // 将面板添加到窗口中
        add(panel, BorderLayout.CENTER);
        add(scrollPane, BorderLayout.EAST);

        // 添加按钮到窗口底部
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(startButton);
        buttonPanel.add(exitButton);
        add(buttonPanel, BorderLayout.SOUTH);

        setSize(600, 400);
        setVisible(true);
    }

    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }

}